import React from 'react';
import { Button, message } from 'antd';
import Modal from 'src/components/Modal/index';
import CommonList from 'src/components/CommonLists/list';
import services from 'src/services/api';

export default class extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'strategyComponent';
    this.operatorWidth = 160;
    this.state = {
      events: [],
    };
    this.editButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          onClick={this.edit.bind(this, record)}
          disabled={record.audit_status === this.AUDIT._WAIT}
        >
          编辑
        </Button>
      );
    };
    this.delButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.audit_status === this.AUDIT._WAIT}
          onClick={this.delete.bind(this, record)}
        >
          删除
        </Button>
      );
    };
    this.copyButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.audit_status === this.AUDIT._WAIT}
          onClick={this.copy.bind(this, record)}
        >
          复制
        </Button>
      );
    };

    this.navButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.audit_status === this.AUDIT._WAIT}
          onClick={this.openVersion.bind(this, record.strategy_id)}
        >
          版本
        </Button>
      );
    };
    this.quoteButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.audit_status === this.AUDIT._WAIT}
          onClick={() => alert('暂未开发，待流程完了才能做')}
        >
          引用
        </Button>
      );
    };
    this.btnGroup = [this.editButton, this.delButton, this.copyButton, this.navButton];
  }
  componentDidMount() {}

  delete(record) {
    Modal.open({
      title: '',
      width: '360px',
      isConfirm: true,
      content: <p>确定要删除本条记录么?</p>,
      isOkBtn: true,
      isCancelBtn: true,
      ok: () => {
        services[this.api].delete(this.getDeleteParam(record)).then((data) => {
          if (data.code === 0) {
            message.success(data.msg);
            this.props.deleteCb(record)
            this.props.queryList();
            Modal.close();
          } else {
            message.error(data.msg);
          }
        });
      },
      cancel: () => {
        Modal.close();
      },
    });
  }

  // 删除时的参数
  getDeleteParam = (record) => {
    return { strategy_id: record.strategy_id };
  };

  // 打开新版本
  openVersion = (id) => {
    this.props.history.push(`/strategy/component/${id}/version`);
  };
}
